package page;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * PageParser class parses HTML code for URLs and words.
 * @author Vladimir Dudnic, Derek Fleener, Michael Groenier
 */
public class PageParser
{
        /**
         * Parses the web page for all URLs.
         *
         * @param page The web page.
         * @return list of addresses.
         */
        public static List<String> parseURLs(Document doc)
        {
                final List<String> pages = new ArrayList<String>();
               
                Elements links = doc.select("a");
                for (Element link : links)
                {
                        pages.add(link.attr("abs:href"));
                }
                return pages;
        }
       
        /**
         * Parses the web page for all used words by extracting only the text.
         *
         * @param url The URL of the page.
         * @return list of words.
         */
        public static List<String> parseWords(Document doc) 
        {
                final List<String> words = new ArrayList<String>();
               
                String text = doc.body().text();
                text = text.toLowerCase();
                String[] parsed = text.split("[ /\\?,.!@#$%^&*(){}+]+");
                for (int i = 0; i < parsed.length; i++) 
                {
                	words.add(parsed[i]);
                }
                return words;
        }
}

